package com.zut.Service.ControllerServe.runCodeImpl;

import com.zut.Pojo.JudgeResult;
import com.zut.Pojo.Request.RunCodeEntity;
import com.zut.Pojo.UserRuncodeCase;
import com.zut.Pojo.language;
import com.zut.Pojo.submissionsBody;
import com.zut.Service.ControllerServe.runCode;
import com.zut.Service.HttpNotWait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class runCodeImpl implements runCode {

    @Autowired
    private HttpNotWait httpNotWait;

    @Override
    public List<UserRuncodeCase> getCaseResult(RunCodeEntity runCodeEntity, String UserId) {
        // 获取输入案例
        List<List<String>> caseStdin = runCodeEntity.getCaseStdin();

        // 并行处理案例，并返回处理结果列表
        List<UserRuncodeCase> collect = caseStdin.parallelStream().map(l -> {
            // 创建请求体对象
            submissionsBody submissionsBody = new submissionsBody();
            // 设置源代码
            submissionsBody.setSource_code(runCodeEntity.getSourceCode());
            // 设置语言ID
            submissionsBody.setLanguage_id(language.valueOf(runCodeEntity.getLanguage()).getId());
            // 设置输入案例
            submissionsBody.setStdin(l.get(1));
            // 发送请求并获取评判结果
            JudgeResult judgeResult = httpNotWait.GoJudge(submissionsBody);
            // 构建 UserRuncodeCase 对象
            return UserRuncodeCase.builder().caseId(l.get(0))
                    .userId(UserId)
                    .problemId(runCodeEntity.getProblemID())
                    .stdout(judgeResult.getStdout())
                    .sourceCode(runCodeEntity.getSourceCode())
                    .language(runCodeEntity.getLanguage())
                    .build();
        }).collect(Collectors.toList());

        // 打印结果列表
        System.out.println(collect);

        // 返回结果列表
        return collect;
    }
}

